# Create your views here.
from django.http import HttpResponse
from rest_framework import status
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView,RetrieveAPIView, GenericAPIView, CreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from complains.utils.pagination import StandardResultsSetPagination
from news.models import NewsCategories, News, Collections, Authorfollow
from news.serializers import CategoriesSerializer, NewslistSerializer, NewsDetailSerializer, NewsCollectionSerializer, \
    NewsSubmitSerializer, NewsfollowdSerializer, CommentSerializer


class NewsCategoriesView(ListAPIView):
    queryset = NewsCategories.objects.all()
    serializer_class = CategoriesSerializer

class CateandnewsView(ListAPIView):
    serializer_class = NewslistSerializer
    pagination_class = StandardResultsSetPagination

    # filter_backends = [OrderingFilter]
    # ordering_fields = ("count", 'create_time')

    def get_queryset(self):
        category_id = self.kwargs['category_id']

        News.objects.filter(category_id=category_id)
        if category_id=='1':
            return News.objects.all().order_by("-click_count")
        else:
            return News.objects.filter(category_id=category_id).order_by("-create_time")

class NewslistView(ListAPIView):
    queryset = News.objects.all().order_by("-click_count")
    serializer_class = NewslistSerializer
    pagination_class = StandardResultsSetPagination

class NewsdetailView(RetrieveAPIView):
    queryset = News.objects.all()
    serializer_class = NewsDetailSerializer


# class NewcommentView()


class NewsCollectionView(GenericAPIView):
    queryset = News.objects.all()
    serializer_class = NewsCollectionSerializer
    permission_classes = [IsAuthenticated]
    # def get_queryset(self):
    #     self.kwargs=['pk']
    def get(self,request,*args,**kwargs):
        user=request.user.id
        new=self.get_object()
        new=new.id
        # serializer=self.get_serializer()
        aimer=Collections.objects.filter(user_id_id=user)
        if aimer:
        # print(aimer)
            for use in aimer:
                if use.news_id:
                    return Response({'message':'不能重复收藏'})
                else:

                    coll=Collections.objects.create(user_id_id=user,news_id_id=new)

                return Response({'message':"收藏成功"})
        else:
            coll=Collections.objects.create(user_id_id=user,news_id_id=new)
            return Response({'message':"收藏成功"})


    # def post(self,request,pk):
    #     user_id=request.user.id
    #     new_id=pk
    #     aimer=Collections.objects.create(user_id_id=user_id,news_id_id=new_id)
    #     aimer.save()
    #
    #     return Response({'message':'ok'})








    # def post(self, request):
    #     serializer = self.get_serializer(data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #     news_id = request.data.get('news_id')
    #     news = News.objects.get(id=news_id)
    #     action = request.data.get('action')
    #     user = request.user
    #     # if action == "1":
    #     #     res = Collections.objects.filter(user=user, news=news)
    #     #     if not res:
    #     #         Collections.objects.create(user=user, news=news)
    #     #     return Response({'show': 1})
    #     # else:
    #     #     res = Collections.objects.get(user=user, news=news)
    #     #     res.delete()
    #     #     return Response({'show': 0})


class NewsSubmitView(CreateAPIView):
    permission_classesion_classes = [IsAuthenticated]
    serializer_class = NewsSubmitSerializer

class NewsfollowView(APIView):
    def post(self,request,pk):
        user_id = request.user.id
        author_id = pk
        aimer = Authorfollow.objects.filter(user_id=user_id)

        if aimer:
            for user in aimer:
                if user.author_id:
                    return Response({'message':'不能重复关注'})
                else:

                    aimer = Authorfollow.objects.create(author_id=author_id, user_id=user_id)

                    return Response({'message':'关注成功'})
        else:
            aimer = Authorfollow.objects.create(author_id=author_id, user_id=user_id)
            return Response({'message':'关注成功'})


# class NewsfollowView(CreateAPIView):
#     permission_classes = [IsAuthenticated]
#     serializer_class = NewsfollowdSerializer
#
#     def post(self, request, *args, **kwargs):
#         print(request.data)
#         return super().post(request, *args, **kwargs)
#
#
#     def create(self, request, *args, **kwargs):
#         print(request.data)
#         serializer = self.get_serializer(data=request.data)
#
#         serializer.is_valid(raise_exception=True)
#         try:
#             Authorfollow.objects.get(user=request.user,author=serializer.validated.data['author'])
#         except:
#             self.perform_create(serializer)
#             headers=self.get_success_headers(serializer.data)
#             return Response({'message':"收藏成功"},status=status.HTTP_201_CREATED,headers=headers)
#         return Response({'message':'收藏失败'},status=status.HTTP_400_BAD_REQUEST)


    # def perform_create(self, serializer):
    #
    #     serializer.save()




class CommentView(CreateAPIView):
    # permission_classes = [IsAuthenticated]
    serializer_class = CommentSerializer
    # def create(self, request, *args, **kwargs):







